﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Text;
namespace Development.Tools
{
    public static class TableHelper
    {
        public static void XmlToCsv()
        {
            if (!Directory.Exists("csv/"))
            {
                Directory.CreateDirectory("csv/");
            }
            string[] files = Directory.GetFiles("/","*.xml");
            foreach (var f in files)
            {
                try
                {
                    string csv = "";
                    XmlDocument xml = new XmlDocument();
                    xml.Load(f);
                    XmlNodeList nodeList = xml.ChildNodes[0].ChildNodes;
                    for (int i = 0; i < nodeList.Count; ++i)
                    {
                        XmlNode nParent = nodeList[i];
                        XmlNodeList nObjs = nParent.ChildNodes;
                        for (int j = 0; j < nObjs.Count; ++j)
                        {
                            var nd = nObjs[j];
                            if (j != nObjs.Count)
                            {
                                csv += nd.InnerText + ",";
                            }
                            else
                            {
                                csv += nd.InnerText + Environment.NewLine;
                            }
                        }
                    }
                    File.WriteAllText("csv/" + Path.GetFileName(f), csv);
                    Console.WriteLine(string.Format("xml to csv success,file = ", f));
                }
                catch (Exception e)
                {
                    Console.WriteLine(string.Format("flie = {0},error = {1}", f , e));
                }
            }
        }
    }
}
